package cn.com.obs;

import java.net.URI;
import java.nio.file.Path;
import java.util.List;

import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
import software.amazon.awssdk.core.ResponseBytes;
import software.amazon.awssdk.core.sync.RequestBody;
import software.amazon.awssdk.core.sync.ResponseTransformer;
import software.amazon.awssdk.http.SdkHttpClient;
import software.amazon.awssdk.http.SdkHttpConfigurationOption;
import software.amazon.awssdk.http.apache.ApacheHttpClient;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.S3Configuration;
import software.amazon.awssdk.services.s3.model.CreateBucketRequest;
import software.amazon.awssdk.services.s3.model.DeleteBucketRequest;
import software.amazon.awssdk.services.s3.model.DeleteObjectRequest;
import software.amazon.awssdk.services.s3.model.GetObjectRequest;
import software.amazon.awssdk.services.s3.model.GetObjectResponse;
import software.amazon.awssdk.services.s3.model.ListObjectsV2Request;
import software.amazon.awssdk.services.s3.model.ListObjectsV2Response;
import software.amazon.awssdk.services.s3.model.PutObjectRequest;
import software.amazon.awssdk.services.s3.model.S3Object;
import software.amazon.awssdk.utils.AttributeMap;

public class ObsS3Utils {

    private static final S3Client s3Client;

    public static final String ACCESS_KEY = "et4AyzzgmstKkvsHSHlI";

    public static final String SECRET_KEY = "XglPajavcwudGWgm5bnQCfgT5GElgI2ZoeVM9oZW";

    public static final String URL = "https://10.106.3.111:32296";

    static {
        AttributeMap attributeMap = AttributeMap.builder().put(SdkHttpConfigurationOption.TRUST_ALL_CERTIFICATES, true).build();
        SdkHttpClient httpClient = ApacheHttpClient.builder()
                                                   .buildWithDefaults(attributeMap);
        s3Client = S3Client.builder()
                           .credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create(ACCESS_KEY, SECRET_KEY)))
                           .endpointOverride(URI.create(URL))
                           .region(Region.US_EAST_1)
                           .httpClient(httpClient)
                           .serviceConfiguration(S3Configuration.builder()
                                                                .pathStyleAccessEnabled(false)
                                                                .chunkedEncodingEnabled(false)
                                                                .build())
                           .build();
    }

    public static void uploadFile(String bucketName, String key, Path localFilePath) {
        s3Client.putObject(PutObjectRequest.builder()
                                           .bucket(bucketName)
                                           .key(key)
                                           .build(), RequestBody.fromFile(localFilePath));
    }

    public static void downloadFile(String bucketName, String key, Path localFilePath) {
        s3Client.getObject(GetObjectRequest.builder()
                                           .bucket(bucketName)
                                           .key(key)
                                           .build(), ResponseTransformer.toFile(localFilePath));
    }

    public static byte[] downloadFileToBytes(String bucketName, String key) {
        ResponseBytes<GetObjectResponse> responseBytes = s3Client.getObject(GetObjectRequest.builder()
                                                                                            .bucket(bucketName)
                                                                                            .key(key)
                                                                                            .build(), ResponseTransformer.toBytes());
        return responseBytes.asByteArray();
    }

    public static List<S3Object> listObjects(String bucketName) {
        ListObjectsV2Request listObjectsRequest = ListObjectsV2Request.builder()
                                                                      .bucket(bucketName)
                                                                      .build();
        ListObjectsV2Response response = s3Client.listObjectsV2(listObjectsRequest);
        return response.contents();
    }

    public static void deleteObject(String bucketName, String key) {
        s3Client.deleteObject(DeleteObjectRequest.builder()
                                                 .bucket(bucketName)
                                                 .key(key)
                                                 .build());
    }

    public static void createBucket(String bucketName) {
        s3Client.createBucket(CreateBucketRequest.builder()
                                                 .bucket(bucketName)
                                                 .build());
    }

    public static void deleteBucket(String bucketName) {
        s3Client.deleteBucket(DeleteBucketRequest.builder()
                                                 .bucket(bucketName)
                                                 .build());
    }
}
